package cn.school.demo.ui.set_love

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import cn.school.demo.MyApplication
import cn.school.demo.R
import cn.school.demo.databinding.FragmentSetLoveBinding
import cn.school.demo.pojo.Hobby
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter


class SetLoveFragment : Fragment() {
    private lateinit var binding: FragmentSetLoveBinding
    private lateinit var viewModel: SetLoveViewModel
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        MyApplication.systemBarSetting(MyApplication.DARK, requireActivity())
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_set_love, container, false)
        viewModel = ViewModelProvider(this).get(SetLoveViewModel::class.java)
        binding.vw = this
        binding.lifecycleOwner = viewLifecycleOwner
        init()
        return binding.root
    }

    private fun init() {
        binding.allLoveHaveText.visibility = View.VISIBLE
        viewModel.tabs.observe(viewLifecycleOwner) {
            binding.myLoveFlowLayout.apply {
                adapter = object : TagAdapter<Hobby>(viewModel.tabs.value) {
                    override fun getView(parent: FlowLayout, position: Int, hobby: Hobby): View {
                        when (viewModel.isEdit.value) {
                            true -> {
                                val tv = LayoutInflater.from(requireContext()).inflate(
                                    R.layout.flow_fragment,
                                    binding.myLoveFlowLayout, false
                                ) as RelativeLayout
                                val viewById =
                                    tv.findViewById(R.id.set_love_text_view_name) as TextView
                                val imageView =
                                    tv.findViewById(R.id.set_love_text_view_delete) as ImageView
                                viewById.text = hobby.hobbyName
                                imageView.setOnClickListener {
                                    val num = viewModel.removeTab(position)
                                    adapter.notifyDataChanged()
                                    binding.allLoveFlowLayout.adapter.notifyDataChanged()
                                    if (num != 0)
                                        binding.allLoveHaveText.visibility = View.INVISIBLE
                                    else binding.allLoveHaveText.visibility = View.VISIBLE
                                }
                                return tv
                            }
                            else -> {
                                val tv = LayoutInflater.from(requireContext()).inflate(
                                    R.layout.flow_un_fragment,
                                    binding.myLoveFlowLayout, false
                                ) as TextView
                                tv.text = hobby.hobbyName
                                return tv
                            }
                        }
                    }
                }
            }
            binding.setLoveProgressBar.visibility = View.GONE
            LogUtils.d(
                "我的爱好列表更新了！",
                viewModel.tabs.value.toString(),
                "运行正常！",
                viewModel.tabs.value!!.size
            )
            viewModel.isEdit.observe(viewLifecycleOwner) {
                //LogUtils.e(viewModel.tabs.value!!.size)
                when (viewModel.isEdit.value) {
                    false -> {
                        viewModel.saveUserTabs()
                        viewModel.save.observe(viewLifecycleOwner) {
                            LogUtils.e(it.success)
                            when (it.success) {
                                true -> {
                                    LogUtils.d("用户爱好修改成功！！")
                                    MyApplication.tabList.clear()
                                    viewModel.tabs.value!!.forEach {
                                        MyApplication.tabList.add(it.hobbyName)
                                    }
                                }
                                else -> {
                                    LogUtils.e(it.message)
                                    ToastUtils.showShort(it.message)
                                }
                            }
                        }
                    }
                    else -> {
                        LogUtils.d("我是编辑")
                    }
                }
            }
        }
        viewModel.tabTitle.observe(viewLifecycleOwner) {
            if (viewModel.tabTitle.value!!.size != 0)
                binding.allLoveHaveText.visibility = View.INVISIBLE
            binding.allLoveFlowLayout.apply {
                adapter = object : TagAdapter<Hobby>(viewModel.tabTitle.value) {
                    override fun getView(parent: FlowLayout, position: Int, hobby: Hobby): View {
                        val tv = LayoutInflater.from(requireContext()).inflate(
                            R.layout.flow_un_fragment,
                            binding.allLoveFlowLayout, false
                        ) as TextView
                        tv.text = hobby.hobbyName
                        return tv
                    }
                }
                setOnTagClickListener { view, position, parent ->
                    when (viewModel.isEdit.value) {
                        true -> {
                            val num = viewModel.addTab(position)
                            adapter.notifyDataChanged()
                            binding.myLoveFlowLayout.adapter.notifyDataChanged()
                            if (num != 0)
                                binding.allLoveHaveText.visibility = View.INVISIBLE
                            else binding.allLoveHaveText.visibility = View.VISIBLE
                        }
                        else -> {}
                    }
                    true
                }
            }
            LogUtils.d(
                "全部爱好列表更新了！",
                viewModel.tabTitle.value.toString(),
                "运行正常！",
                viewModel.tabTitle.value!!.size
            )
        }

    }

    fun gotoBack() {
        MyApplication.apply {
            when (navigateThing["SetLove"]) {
                "MyInfo to SetLove" -> {
                    goto(
                        R.id.action_SetLove_to_My,
                        findNavController(),
                        requireFragmentManager()
                    )
                    navigateThing.remove("SetLove")
                }
                "Article to SetLove" -> {
                    goto(
                        R.id.action_SetLove_to_Article,
                        findNavController(),
                        requireFragmentManager()
                    )
                    navigateThing.remove("SetLove")
                }
                else -> LogUtils.e("SetLoveFragment出现严重错误！")
            }
        }
    }

    fun modifyTabs() {
        viewModel.isEdit.value = !viewModel.isEdit.value!!
        binding.apply {
            if (viewModel.tabTitle.value!!.size != 0)
                allLoveFlowLayout.adapter.notifyDataChanged()
            myLoveFlowLayout.adapter.notifyDataChanged()
        }
        when (viewModel.isEdit.value) {
            true -> {
                binding.setLoveButton.apply {
                    text = "完成"
                    setBackgroundResource(R.drawable.button_shape_set_love_selected)
                    setTextColor(Color.WHITE)
                }
                binding.setLoveMyLoveText.text = "我的爱好(前五项必选，不可删除!)"
            }
            false -> {
                binding.setLoveButton.apply {
                    text = "编辑"
                    setBackgroundResource(R.drawable.button_shape_set_love_unselected)
                    setTextColor(Color.RED)
                }
                binding.setLoveMyLoveText.text = "我的爱好"
            }
            else -> LogUtils.e("系统出错了")
        }
    }
}
